//
//  XLEmoticCollectionViewCell.swift
//  XLVeibo
//
//  Created by macbook on 16/7/5.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit


class XLEmoticCollectionViewCell: UICollectionViewCell {
    
    
    var emoticons:[XLEmoticon]?{
    
        didSet{
            //当每次外界给其赋值的时候  要把cell中的20个按钮全部隐藏，因为cell复用的问题
            //遍历按钮
            for button in emotionBtn {
                
                button.hidden = true
            }
            
            //遍历外界传入的一维数组
            for (i,emoticon) in emoticons!.enumerate() {
                
                let btn = emotionBtn[i]
                
                //显示btn
                btn.hidden = false
                btn.emoticon = emoticon
                //type == 0 代表图片
                //tyoe == 1 代表字符串
                if emoticon.isEmoji {
                    
                    
                }else{
                
                let path = emoticon.path ?? ""
                    let png = emoticon.png ?? ""
                    let name = path + png
                    let iamge = UIImage(named: name,inBundle: XLEmoticonShardTools.shardTools.emoticonBundle,compatibleWithTraitCollection: nil)
                    //代表图片表情
                    btn.setImage(iamge, forState: UIControlState.Normal)
                }
            }
            
         }
    }
    
  
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    func setupUI(){
    backgroundColor = UIColor.whiteColor()
        
        addChildBtn()
        
        
        contentView.addSubview(label)
        
        contentView.addSubview(deleteBtn)
        
        label.snp_makeConstraints { (make) in
            
            make.center.equalTo(contentView)
        }
        
    }
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //按钮的宽度和高度
        let w = (KScreenW - 10) / 7
        let h = (216 - 37 - 20) / 3
        
        //变量按钮循环添加
        for (i,button) in emotionBtn.enumerate() {
            
            let x = i % 7
            let y = i / 7
            
            //设置frame
            button.frame = CGRect(x: CGFloat(x) * w + 5, y: CGFloat(y) * CGFloat(h), width: w, height: CGFloat(h))
        }
        
        deleteBtn.frame = CGRect(x: KScreenW - 5 - w, y: 2 * CGFloat(h), width: w, height: CGFloat(h))
    }
    
    
    
    var indexPath:NSIndexPath?{
    
        didSet{
        
            label.text = "第\(indexPath?.section ?? 0)组 第\(indexPath?.item ?? 0)页"
         }
    }
    
    //创建数组用来装按钮
    var emotionBtn:[XLEmoticonBtn] = [XLEmoticonBtn]()
    //循环创建按钮
    func addChildBtn(){
    
        for _ in 0...19{
        
             let btn = XLEmoticonBtn()
            //添加点击事件
            btn.addTarget(self, action: #selector(XLEmoticCollectionViewCell.btnClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
 
             btn.backgroundColor = UIColor.whiteColor()
            emotionBtn.append(btn)
            contentView.addSubview(btn)
         }
    }
    
    //按钮的点击事件
    @objc private func btnClick(button: XLEmoticonBtn) {
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonButtonNoti, object: button.emoticon)
    }
    
    
    //删除按钮的实现，系统通知
    @objc private func deleteBtnClick(){
    
    NSNotificationCenter.defaultCenter().postNotificationName(EmoticonDeleteNoti, object: nil)
        
    }
    
    //懒加载显示第几组，第几页的label
    private lazy var label:UILabel = {
    
        let label = UILabel(textColor: UIColor.redColor(), textFont: 30)
        label.textAlignment = .Center
         return label
    
    }()
    
    //删除按钮
    private lazy var deleteBtn:UIButton = {
    
        let btu = UIButton()
        btu.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btu.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        btu.addTarget(self, action: #selector(XLEmoticCollectionViewCell.deleteBtnClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btu
    }()
    
    
}





